package com.chengwei.ch.busdemo.activity;


import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.chengwei.ch.busdemo.R;
import com.chengwei.ch.busdemo.adapter.SiteAdapter;
import com.chengwei.ch.busdemo.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

@SuppressLint("SetTextI18n")
public class BuyTicketActivity extends BaseActivity {

    private LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
    private SiteAdapter mSiteAdapter = new SiteAdapter(R.layout.item_site, new ArrayList<>());

    private RecyclerView rvListSite;
    private TextView tvStartSite, tvAmount, tvNum, tvPrice,tvRemainNum;
    private Button btnDecrease, btnIncrease, btnCommit;
    private int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_buy);
        initView();
        initClickListener();
    }

    private void initView() {
        rvListSite = findViewById(R.id.rvListSite);
        tvStartSite = findViewById(R.id.tvStartSiteBuyTicket);
        tvAmount = findViewById(R.id.tvAmountItemPersonNum);
        tvNum = findViewById(R.id.tvNumBuyTicket);
        tvPrice = findViewById(R.id.tvPriceBuyTicket);
        tvRemainNum = findViewById(R.id.tvRemainNumSiteBuyTicket);
        btnDecrease = findViewById(R.id.btnDecreaseItemPersonNum);
        btnIncrease = findViewById(R.id.btnIncreaseItemPersonNum);
        btnCommit = findViewById(R.id.btnConfirmBuyTicket);
        String siteNum = "本班次剩余8个站";
        String num = "余票:420张";
        String price = "单票价:￥20";
        SpannableStringBuilder style = new SpannableStringBuilder(num);
        style.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorRightE2)), 3, num.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableStringBuilder style1 = new SpannableStringBuilder(price);
        style1.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorRightE2)), 4, price.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        SpannableStringBuilder style2 = new SpannableStringBuilder(siteNum);
        style2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorRight)), 5, price.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tvNum.setText(style);
        tvPrice.setText(style1);
        tvRemainNum.setText(style2);
        initRecycler();
    }

    private void initClickListener() {
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        btnCommit.setOnClickListener(this);
    }

    private void initRecycler() {
        List<String> listSite = new ArrayList<>();
        listSite.add("大脚村");
        listSite.add("大合江");
        listSite.add("上坝田上坝田上坝田上坝田上坝田上坝田上坝田");
        listSite.add("鸡邑铺");
        listSite.add("金牛");
        listSite.add("驾考中心");
        listSite.add("马厂");
        listSite.add("平坡");
        listSite.add("大脚村");
        listSite.add("大合江");
        listSite.add("上坝田");
        listSite.add("鸡邑铺");
        listSite.add("金牛");
        listSite.add("驾考中心");
        listSite.add("马厂");
        listSite.add("平坡");
        rvListSite.setLayoutManager(mLinearLayoutManager);
        rvListSite.setAdapter(mSiteAdapter);
        mSiteAdapter.setNewData(listSite);
        mSiteAdapter.setOnItemClickListener((adapter, view, position) -> {
        });
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        switch (v.getId()) {
            case R.id.btnDecreaseItemPersonNum: {
                count = Integer.parseInt(tvAmount.getText().toString());
                if (count == 1) {
                    ToastUtils.showShort("票数已是最小值");
                    return;
                }
                count -= 1;
                tvAmount.setText("" + count);
                break;
            }
            case R.id.btnIncreaseItemPersonNum: {
                count = Integer.parseInt(tvAmount.getText().toString());
                if (count >= 1) {
                    count += 1;
                    tvAmount.setText("" + count);
                } else ToastUtils.showShort("票数不能是负数");
                break;
            }
            case R.id.btnConfirmBuyTicket: {
                intent.setClass(this, BuyTicketScanInfActivity.class);
                startActivity(intent);
                finish();
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
